<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Custom Session Handlers</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="session.idpassing.html">Passing the Session ID</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="session.upload-progress.html">Session Upload Progress</a></div>
 <div class="up"><a href="session.examples.html">范例</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="session.customhandler" class="section">
  <h2 class="title">Custom Session Handlers</h2>
  <p class="para">
   To implement database storage, or any other storage method, you
   will need to use <span class="function"><a href="function.session-set-save-handler.html" class="function">session_set_save_handler()</a></span> to
   create a set of user-level storage functions. As of PHP 5.4.0 you may create session handlers
   using the <a href="class.sessionhandlerinterface.html" class="classname">SessionHandlerInterface</a> or extend internal PHP handlers by inheriting
   from <a href="class.sessionhandler.html" class="classname">SessionHandler</a>.
  </p>
  <p class="para">
   The callbacks specified in <span class="function"><a href="function.session-set-save-handler.html" class="function">session_set_save_handler()</a></span> are methods
   called by PHP during the life-cycle of a session: <em><code class="parameter">open</code></em>, <em><code class="parameter">read</code></em>,
   <em><code class="parameter">write</code></em> and <em><code class="parameter">close</code></em> and for the housekeeping tasks:
   <em><code class="parameter">destroy</code></em> for deleting a session and <em><code class="parameter">gc</code></em> for periodic garbage
   collection.
  </p>
  <p class="para">
   Therefore, PHP always requires session save handlers. The default is usually the
   internal &#039;files&#039; save handler. A custom save handler can be set using
   <span class="function"><a href="function.session-set-save-handler.html" class="function">session_set_save_handler()</a></span>. Alternative internal save handlers are also be
   provided by PHP extensions, such as <em><code class="parameter">sqlite</code></em>,
   <em><code class="parameter">memcache</code></em> and <em><code class="parameter">memcached</code></em> and can be set with
   <a href="session.configuration.html#ini.session.save-handler" class="link">session.save_handler</a>.
  </p>
  <p class="para">
   When the session starts, PHP will internally call the <em><code class="parameter">open</code></em> handler followed by the
   <em><code class="parameter">read</code></em> callback which should return an encoded string exactly as it was originally
   passed for storage. Once the <em><code class="parameter">read</code></em> callback returns the encoded string, PHP will
   decode it and then populate the resulting array into the <var class="varname"><var class="varname"><a href="reserved.variables.session.html" class="classname">$_SESSION</a></var></var> superglobal.
  </p>
  <p class="para">
   When PHP shuts down (or when <span class="function"><a href="function.session-write-close.html" class="function">session_write_close()</a></span> is called),
   PHP will internally encode the <var class="varname"><var class="varname"><a href="reserved.variables.session.html" class="classname">$_SESSION</a></var></var> superglobal and pass this
   along with the session ID to the the <em><code class="parameter">write</code></em> callback.
   After the <em><code class="parameter">write</code></em> callback has finished, PHP will internally invoke the
   <em><code class="parameter">close</code></em> callback handler.
  </p>
  <p class="para">
   When a session is specifically destroyed, PHP will call the <em><code class="parameter">destroy</code></em> handler with
   the session ID.
  </p>
 <p class="para">
   PHP will call the <em><code class="parameter">gc</code></em> callback from time to time to
   expire any session records according to the set max lifetime of a session.
   This routine should delete all records from persistent storage which were
   last accessed longer than the <em><code class="parameter">$lifetime</code></em>.
 </p>
 </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="session.idpassing.html">Passing the Session ID</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="session.upload-progress.html">Session Upload Progress</a></div>
 <div class="up"><a href="session.examples.html">范例</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
